package com.jsptags.navigation.pager;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;

@SuppressWarnings("serial")
public abstract class JumpTagSupport extends PageTagSupport {

	static final String CURRENT = "current";
	static final String INDEXED = "indexed";
	private String unless;

	public final void setUnless(String s) throws JspException {
		if (s != null && !"current".equals(s) && !"indexed".equals(s)) {
			throw new JspTagException(
					"value for attribute \"unless\" must be either \"current\" or \"indexed\".");
		} else {
			unless = s;
			return;
		}
	}

	public final String getUnless() {
		return unless;
	}

	public int doStartTag() throws JspException {
		super.doStartTag();
		int i = getJumpPage();
		if ("current".equals(unless)) {
			if (i == pagerTag.getPageNumber())
				return 0;
		} else if ("indexed".equals(unless)) {
			int j = pagerTag.getFirstIndexPage();
			int k = pagerTag.getLastIndexPage(j);
			if (i >= j && i <= k)
				return 0;
		}
		setPageAttributes(i);
		return 1;
	}

	public void release() {
		unless = null;
		super.release();
	}

	protected abstract int getJumpPage();

	public JumpTagSupport() {
		unless = null;
	}
}
